package com.fh.fruit.controller.activity;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.entity.activity.ActivityLabel;
import com.fh.fruit.service.activity.ActivityLabelManager;
import com.fh.fruit.util.DateUtil;
import com.fh.util.Const;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/activityLabel")
public class ActivityLabelController extends BaseController {
	String menuUrl = "activityLabel/listActivityLabel.do"; // 地址(权限用)

	@Resource(name = "activityLabelService")
	private ActivityLabelManager activityLabelManager;
	
	
	/**活动标签列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listActivityLabel")
	public ModelAndView listActivityLabel(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(StringUtils.isNotEmpty(keywords)){
			pd.put("keywords", keywords.trim());
		}
		page.setPd(pd);
		page.setShowCount(5);
		List<ActivityLabel>	activityLabelList = activityLabelManager.getActivityLabel(page);
		mv.addObject("activityLabelList", activityLabelList);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		mv.setViewName("farm/activity/activityLabel_list");
		return mv;
	}
	
	/**
	 * 删除活动标签
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delActivityLabel" )
	@ResponseBody
	public String delActivityLabel(HttpServletRequest requset) {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "删除活动标签");  
		String labelCode = requset.getParameter("labelCode");
		try {
			int num = activityLabelManager.deleteActivityLabel(labelCode);
			if(num>0){
				return "success";
			}else{
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
	}
	
	/**
	 * 去新增活动标签页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAddActivityLabel")
	public ModelAndView goAddActivityLabel(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		mv.addObject("msg", "saveAddActivityLabel");
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		mv.setViewName("farm/activity/activityLabel_edit");
		return mv;
	}
	
	/**
	 * 新增活动标签
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveAddActivityLabel")
	public ModelAndView saveAddActivityLabel(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "新增活动标签");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("createId", user.getUSER_ID()); // 创建人
		pd.put("createTime", DateUtil.getTime()); // 创建时间
		int num=activityLabelManager.addActivityLabel(pd);
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 去修改活动标签页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goEditActivityLabel")
	public ModelAndView goEditG(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String labelCode=pd.getString("code"); 
		ActivityLabel activityLabel = activityLabelManager.getActivityLabelByLabelCode(labelCode);
		mv.addObject("activityLabel", activityLabel);
		mv.addObject("code", labelCode);
		mv.addObject("msg", "editActivityLabel");
		mv.setViewName("farm/activity/activityLabel_edit");
		return mv;
	}
	/**
	 * 修改活动标签
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editActivityLabel")
	public ModelAndView editActivityLabel(HttpServletRequest requset) throws Exception {

		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "修改活动标签");
		ModelAndView mv = this.getModelAndView();
		PageData pd = null;
		try {
			pd = this.getPageData();
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("updateId", user.getUSER_ID()); // 修改人
			pd.put("updateTime", DateUtil.getTime()); // 修改时间
			int num=activityLabelManager.updateActivityLabel(pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			mv.addObject("msg", "failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 批量删除商品
	 * 
	 * @return
	 * @throws Exception
	 *//*
	@RequestMapping(value = "/delGoods")
	@ResponseBody
	public String delGoods(HttpServletRequest requset) {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "删除商品");
		PageData pd = this.getPageData();
		try {
			goodsService.delGoods(pd);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
	}
	
	*/
}
